/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./vErrorDialog.h"
#include <stdio.h>


namespace Vendetta {
	vString vErrorDialog::mError = "";
	HINSTANCE vErrorDialog::mhInstance;

	vErrorDialog::vErrorDialog(HINSTANCE hInstance, HWND hWndOwner, const vString& errorMsg)
		: vAbstractModalDialog(hWndOwner) {
		mhInstance = hInstance;
		vErrorDialog::mError = errorMsg;
	}

	vErrorDialog::vErrorDialog(const vString& errorMsg)
		: vAbstractModalDialog(vNull) {
			mhInstance = ::GetModuleHandle(vNull);
			vErrorDialog::mError = errorMsg;
	}

	vInt vErrorDialog::Exec() {
		return (vInt) ::DialogBoxParam(mhInstance, MAKEINTRESOURCE(IDD_ERROR_DIALOG), 
			mhOwner, (DLGPROC) dialogProc, (LPARAM) this);
	}

	LRESULT CALLBACK vErrorDialog::dialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
		static bool bShowDetails = false;
		static HBITMAP hbmObraz, hbmOld, hbmMaska;
		static HDC hdcNowy;
		
		switch(message)	{
		case WM_INITDIALOG: {
				::InitCommonControls();
				::SetDlgItemText(hWnd, IDC_ERR_ERROR_MSG_TA, vErrorDialog::mError.ToConstCharPtr());
				toggleDetails(hWnd, bShowDetails);
				hbmObraz = ::LoadBitmap(mhInstance, MAKEINTRESOURCE(IDB_ERROR_BMP));
				hbmMaska = ::LoadBitmap(mhInstance, MAKEINTRESOURCE(IDB_ERROR_MASK_BMP));
				HDC hdcOkno = ::GetDC(hWnd);
				hdcNowy = ::CreateCompatibleDC(hdcOkno);
				::ReleaseDC(hWnd, hdcOkno);
			}
			break;
		case WM_CLOSE:
			::EndDialog(hWnd, 1);
			break;
		case WM_DESTROY:
			::SelectObject(hdcNowy, hbmOld);
			::DeleteDC(hdcNowy);
			::DeleteObject(hbmMaska);
			::DeleteObject(hbmObraz);
			break;
		case WM_DRAWITEM: {
				LPDRAWITEMSTRUCT pdis;
				HDC hDC;
				HWND hDrawItem;

				pdis = (LPDRAWITEMSTRUCT) lParam;
				hDC = pdis->hDC;
				hDrawItem = pdis->hwndItem;

				BITMAP bmInfo;
				::GetObject(hbmObraz, sizeof(bmInfo), &bmInfo);

				hbmOld = (HBITMAP) ::SelectObject(hdcNowy, hbmMaska);
				::BitBlt(hDC, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
				::SelectObject(hdcNowy, hbmObraz);
				::BitBlt(hDC, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
			}
			break;
		case WM_COMMAND:
			switch (LOWORD (wParam)) {
			case IDOK:
				::EndDialog(hWnd, 1);
				break;
			case IDC_ERR_DETAILS_BTN:
				bShowDetails = !bShowDetails;
				toggleDetails(hWnd, bShowDetails);
				break;
			default:
				break;
			}
			break;
		}
		return false;
	}

	vVoid vErrorDialog::toggleDetails(HWND hwnd, vBool bVisible) {
		vRect rect;

		HWND hErrorTA = ::GetDlgItem(hwnd, IDC_ERR_ERROR_MSG_TA);
		::GetWindowRect(hwnd, &rect);
		::ShowWindow(hErrorTA, bVisible ? SW_SHOW : SW_HIDE);
		rect.bottom = bVisible ? 393 : 211;
		::SetWindowPos(hwnd, 0, 0, 0, rect.right - rect.left, rect.bottom, SWP_NOZORDER | SWP_NOMOVE);
	}
}